package com.jiaobuchong.kafka.producer.sync;

import com.google.gson.Gson;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Properties;

public class ConsumerProducer {
	public static void main(String[] args) {
	    String topicName = "msg.bidding-manage-consumer.async";

        Properties props = new Properties();
        props.put("bootstrap.servers", "10.21.32.34:9092");
        props.put("acks", "all");
        props.put("retries", 0);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        Producer<String, String> producer = new KafkaProducer<>(props);
        BiddingConsumerMsgBO msgBO = new BiddingConsumerMsgBO();
        CreativeSearchParamBO searchParamBO = new CreativeSearchParamBO();
        searchParamBO.setCompany("钱包服务接口测试");
        msgBO.setCreativeSearchParamBO(searchParamBO);

        Gson gson = new Gson();
        String message = gson.toJson(msgBO);

        ProducerRecord<String, String> record = new ProducerRecord<>(topicName, message);
        try {
            // 同步发送，然后等待 Kafka 的响应
            RecordMetadata metadata = producer.send(record).get();
            System.out.println("Message is sent to  Partition no: " + metadata.partition() +
            " and offset " + metadata.offset());

            System.out.println("SynchronousProducer Completed with success");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("SynchronousProducer failed with an exception");
        } finally {
            producer.close();
        }
    }
}
